<?php


namespace app\common\cachemodel\zl1_relation;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl1RelationTag extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl1_relation_tag';

    protected $cacheModel = 'astr_community.relation_tag';

    /**
     * 获取关系标签
     * @param int $type
     * @param array $field
     * @return array
     * @throws \Exception
     */
    public function getRelationTag(int $type, array $field = [])
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, 0)->setListKeyType($type)->setSort('DESC')->ZsGetIocData('list', function () use ($type)
        {
            if ($type)
            {
                $where = ['type' => $type, 'status' => 1];
            } else
            {
                $where = ['status' => 1];
            }
            try
            {
                $data = $this->where($where)->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);

        return $this->batchGetInfoByIds(array_keys($ids), $field);
    }


    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        $field = 'type,status,name,sort';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'type', 'sort', 'status']);
        }

        return $resData;
    }
}
